using UnityEngine;
using UnityEngine.UI;

public class HealthBar : MonoBehaviour
{
    [Tooltip("目标 Player 对象，必须挂 SCT_TankMovement 脚本")]
    public GameObject player;

    [Tooltip("最大血量（与 SCT_TankMovement 中的满血一致）")]
    public int maxHealth = 100;

    private SCT_TankMovement tankMovement;
    private Image healthImage;

    void Start()
    {
        if (player != null)
        {
            tankMovement = player.GetComponent<SCT_TankMovement>();
        }

        healthImage = GetComponent<Image>();

        if (healthImage == null)
        {
            Debug.LogError("HealthBar 脚本所在对象上没有 Image 组件！");
        }

        if (tankMovement == null)
        {
            Debug.LogError("目标对象上找不到 SCT_TankMovement 组件！");
        }
    }

    void Update()
    {
        if (tankMovement != null && healthImage != null)
        {
            float healthPercent = Mathf.Clamp01((float)tankMovement.currentHealth / maxHealth);
            healthImage.fillAmount = healthPercent;
        }
    }
}
